package com.example.baselib.utils;

import com.example.baselib.utils.log.LogUtils;
import ohos.app.AbilityContext;
import ohos.app.Environment;
import ohos.data.usage.DataUsage;
import ohos.data.usage.MountState;

import java.io.File;

/**
 * Description:  TODO
 * Author: admin
 * CreateDate: 2021/4/6 11:25
 */
public class SDCardUtils {

    /**
     * 已经安装
     *
     * @return
     */
    public static boolean isMounted() {
        MountState status = DataUsage.getDiskMountedStatus();
        return status == MountState.DISK_MOUNTED;
    }

    /**
     * 是否为热插拔
     *
     * @return
     */
    public static boolean isPluggable() {
        return DataUsage.isDiskPluggable();
    }

    /**
     * 获取储存根目录
     *
     * @return
     */
    public static String getPhoneRootDir() {
        if (isMounted()) {
            String root = System.getProperty("user.dir");
            LogUtils.e("System.getProperty Path = %s", root);
            return root;
        }
        return null;
    }

    /**
     * 从根目录创建文件夹
     *
     * @param dir 子路径
     * @return 路径目录
     */
    public static File createDirFromRoot(String dir) {
        String root = getPhoneRootDir();
        if (null == root || root.length() == 0) {
            LogUtils.e("获取根目录失败");
            return null;
        }
        File newDir = new File(root, dir);
        if (!newDir.exists() || !newDir.isDirectory()) {
            newDir.mkdirs();
        }
        return newDir;
    }

    public static File getExternalFilesDir(AbilityContext context, String type) {
        if (context == null) {
            return null;
        }
        if (null == type || type.length() == 0) {
            return null;
        }
        return context.getExternalFilesDir(type);
    }


    public static File getDownloadsDir(AbilityContext context) {
        return getExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS);
    }

    public static File getDocumentsDir(AbilityContext context) {
        return getExternalFilesDir(context, Environment.DIRECTORY_DOCUMENTS);
    }

    public static File getMusicDir(AbilityContext context) {
        return getExternalFilesDir(context, Environment.DIRECTORY_MUSIC);
    }

    public static File getPicturesDir(AbilityContext context) {
        return getExternalFilesDir(context, Environment.DIRECTORY_PICTURES);
    }

    public static File getMoviesDir(AbilityContext context) {
        return getExternalFilesDir(context, Environment.DIRECTORY_MOVIES);
    }


}
